package com.windchime.blog.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.Resource;
import com.windchime.blog.entity.ResourceType;
import com.windchime.blog.entity.vo.ResourceSearch;
import com.windchime.blog.entity.vo.TypeOrTagVo;
import com.windchime.blog.result.R;
import com.windchime.blog.service.ResourceService;
import com.windchime.blog.service.ResourceTypeService;
import com.windchime.blog.utils.StringSplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author WindChime
 * @since 2021-08-08
 */
@RestController
@RequestMapping("/admin/resource")
public class AdminResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private ResourceTypeService resourceTypeService;

    /**
     * 分页查询-带条件
     * @param page 当前页
     * @param limit 当前页条目数
     * @param resourceSearch 条件
     * @return resources
     */
    @GetMapping("/selectResourceList")
    public R selectResourceList(Integer page, Integer limit, ResourceSearch resourceSearch) {
        PageInfo<Resource> resources = resourceService.selectResourceList(page, limit, resourceSearch);
        return R.ok().code(0).msg("").count(resources.getTotal()).data("resources", resources.getList());
    }

    /**
     * 添加资源
     * @param resource 资源
     * @return true
     */
    @PostMapping("/saveResource")
    public R saveResource(Resource resource) {
        boolean b = resourceService.save(resource);
        if (b) {
            return R.ok().msg("添加成功");
        } else {
            return R.error().msg("添加失败");
        }
    }

    /**
     * 根据资源ID查询一级分类
     * @param id 资源ID
     * @return oneType
     */
    @GetMapping("/selectOneChecked/{id}")
    public R selectOneChecked(@PathVariable Long id) {
        Resource resource = resourceService.getById(id);
        ResourceType resourceType = resourceTypeService.getById(resource.getOneTypeId());
        List<TypeOrTagVo> typeOrTagVos = new ArrayList<>();
        typeOrTagVos.add(new TypeOrTagVo(resourceType.getName(), resourceType.getId()));
        return R.ok().data("oneChecked", typeOrTagVos);
    }

    /**
     * 根据资源ID查询每个一级分类ID下有什么二级分类
     * @param id 资源ID
     * @return twoTypeList
     */
    @GetMapping("/selectTwoTypeList/{id}")
    public R selectTwoTypeList(@PathVariable Long id) {
        Resource resource = resourceService.getById(id);
        List<TypeOrTagVo> typeOrTagVos = new ArrayList<>();
        ResourceType resourceType = resourceTypeService.getById(resource.getOneTypeId());
        List<ResourceType> resourceTypes = resourceTypeService.list(new QueryWrapper<ResourceType>().eq("flag", true).eq("parent_id", resourceType.getId()));
        if (resourceTypes.size() == 0) {
            typeOrTagVos.add(new TypeOrTagVo("无分类", 0));
        } else {
            typeOrTagVos = resourceTypes.stream().map(res -> new TypeOrTagVo(res.getName(), res.getId())).collect(Collectors.toList());
        }
        return R.ok().data("twoTypeList", typeOrTagVos);
    }

    /**
     * 根据资源ID查询二级分类
     * @param id 资源ID
     * @return twoType
     */
    @GetMapping("/selectTwoChecked/{id}")
    public R selectTwoChecked(@PathVariable Long id) {
        Resource resource = resourceService.getById(id);
        ResourceType resourceType = resourceTypeService.getById(resource.getTwoTypeId());
        List<TypeOrTagVo> typeOrTagVos = new ArrayList<>();
        if (resourceType == null) {
            typeOrTagVos.add(new TypeOrTagVo("无分类", 0));
        } else {
            typeOrTagVos.add(new TypeOrTagVo(resourceType.getName(), resourceType.getId()));
        }
        return R.ok().data("twoChecked", typeOrTagVos);
    }

    /**
     * 修改资源
     * @param resource 资源
     * @return true
     */
    @PostMapping("/editResource")
    public R editResource(Resource resource) {
        boolean b = resourceService.updateById(resource);
        if (b) {
            return R.ok().msg("修改成功");
        } else {
            return R.error().msg("修改失败");
        }
    }

    /**
     * 删除资源
     * @param id 资源ID
     * @return true
     */
    @GetMapping("/deleteResource/{id}")
    public R deleteResource(@PathVariable Long id) {
        boolean b = resourceService.removeById(id);
        if (b) {
            return R.ok().msg("删除成功");
        } else {
            return R.error().msg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids 资源ID字符串数组
     * @return true
     */
    @GetMapping("/deleteResources/{ids}")
    public R editResource(@PathVariable String ids) {
        List<Long> idList = StringSplitUtil.split(ids, ",");
        boolean b = resourceService.removeByIds(idList);
        if (b) {
            return R.ok().msg("删除成功");
        } else {
            return R.error().msg("删除失败");
        }
    }
}

